package com.winit.deploy.util;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.DateUtil;
import com.winit.common.util.SpringContextHolder;
import com.winit.core.mq.exchange.MessageExchange;
import com.winit.core.mq.message.Message;
import com.winit.core.task.ChainBaseTask;
import com.winit.core.task.Task;
import com.winit.deploy.bean.DeployTaskStatus;
import com.winit.deploy.handler.TaskStatusHandler;
import org.springframework.util.StringUtils;

import java.util.*;

public class DeployHandel {

    public static ChainBaseTask.TaskHandler getDeployHandler(final String envName, final String areaName, final String queueName
            , final String projectName, final String editionNum) {
        ChainBaseTask.TaskHandler handler = new ChainBaseTask.TaskHandler() {
            @Override
            public void handleTaskNodeStatus(String taskId, List<String> taskNodeStatus) {
                DeployTaskStatus status = DeployFileUtil.handelAreaTaskStatus(envName, areaName, taskId, taskNodeStatus, projectName, editionNum, null, null);
                Map head = new HashMap();
                head.put("queueName", queueName);
                head.put("envName", envName);
                head.put("areaName", areaName);
                head.put("type", "areaTaskStatus");

                JSONObject object = new JSONObject();
                object.put("id", taskId);
                object.put("status", status.getStatus());

                Message<JSONObject> message = new Message<>(head, object);
                MessageExchange messageExchange = SpringContextHolder.getBean(MessageExchange.class);
                messageExchange.sendMessage(queueName, message);
            }

            @Override
            public void handleChainTaskStatus(String taskId, String taskStatus) {
                if (!Task.TaskStatus.STATUS_RUNNING.getValue().equals(taskStatus)
                        && !Task.TaskStatus.STATUS_PAUSE.getValue().equals(taskStatus)
                        && !Task.TaskStatus.STATUS_CONTINUE.getValue().equals(taskStatus)) {
                    TaskStatusHandler taskStatusHandler = TaskStatusHandler.getInstance();
                    taskStatusHandler.handleAreaTaskStatus(envName, areaName, taskId, queueName);
                }

                if (Task.TaskStatus.STATUS_PAUSE.getValue().equals(taskStatus)) {
                    Map head = new HashMap();
                    head.put("queueName", queueName);
                    head.put("envName", envName);
                    head.put("areaName", areaName);
                    head.put("type", "areaTaskStatus");

                    JSONObject object = new JSONObject();
                    object.put("id", taskId);
                    object.put("confirm", "true");
                    Message<JSONObject> me = new Message<>(head, object);
                    MessageExchange messageExchange = SpringContextHolder.getBean(MessageExchange.class);
                    messageExchange.sendMessage(queueName, me);
                }

                if (Task.TaskStatus.STATUS_CONTINUE.getValue().equals(taskStatus)
                        || Task.TaskStatus.STATUS_STOP.getValue().equals(taskStatus)) {
                    Map head = new HashMap();
                    head.put("queueName", queueName);
                    head.put("envName", envName);
                    head.put("areaName", areaName);
                    head.put("type", "areaTaskStatus");

                    JSONObject object = new JSONObject();
                    object.put("id", taskId);
                    object.put("confirm", "false");
                    Message<JSONObject> me = new Message<>(head, object);
                    MessageExchange messageExchange = SpringContextHolder.getBean(MessageExchange.class);
                    messageExchange.sendMessage(queueName, me);
                }

                System.out.println(DateUtil.getCurDateStr(DateUtil.FORMAT_SS) + taskId + ":chainBaseTask taskStatus:" + taskStatus);
            }

            @Override
            public void handleChainTaskMessage(String taskId, String message) {
                System.out.println(DateUtil.getCurDateStr(DateUtil.FORMAT_SS) + taskId + ":chainBaseTask message:" + message);

                Map head = new HashMap();
                head.put("queueName", queueName);
                head.put("envName", envName);
                head.put("areaName", areaName);
                head.put("type", "areaTaskStatus");

                JSONObject object = new JSONObject();
                object.put("id", taskId);
                object.put("statusMsg", message);

                Message<JSONObject> me = new Message<>(head, object);
                MessageExchange messageExchange = SpringContextHolder.getBean(MessageExchange.class);
                messageExchange.sendMessage(queueName, me);

                DeployFileUtil.handelAreaTaskStatus(envName, areaName, taskId, null, projectName, editionNum, null, message);
            }
        };
        return handler;
    }
}
